<template>
  <div>
    <el-dialog
      :title="ruleForm.id == '' ? '添加离线分组' : '编辑离线分组'"
      :visible.sync="addEditIsDialog_c"
      @close="resetForm('formName')"
      width="800px"
    >
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="formName"
        label-width="120px"
        class="demo-ruleForm"
        :class="{ 'form-rules-show': seeType }"
      >
        <el-row>
          <el-col :span="24">
            <el-form-item label="公司名称：" prop="companyId">
              <el-select
                v-model="ruleForm.companyId"
                placeholder="请选择公司名称"
                clearable
                @change="companyChange"
                :disabled="seeType || userType == 2"
              >
                <el-option
                  v-for="item in companyArray"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="离线模板：" prop="modelOfflineSettingsId">
              <el-select
                v-model="ruleForm.modelOfflineSettingsId"
                placeholder="请选择离线模板"
                clearable
                :disabled="seeType"
              >
                <el-option
                  v-for="item in modelOfflineSettingsArray"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="设备分组：" prop="deviceGroupId">
              <el-select
                v-model="ruleForm.deviceGroupId"
                placeholder="请选择设备分组"
                clearable
                :disabled="seeType"
              >
                <el-option
                  v-for="item in deviceGroupArray"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="resetForm('formName')">取 消</el-button>
        <el-button type="primary" @click="submitForm('formName')"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>
<script type="text/ecmascript-6">
import {
  getModelOfflineGroup,
  saveModelOfflineGroup,
  getModelOfflineSettingss,
} from "@/api/offline";
import { getCompanyInfoTree } from "@/api/company";
import { getDeviceGroups } from "@/api/alarm";

export default {
  props: {
    addEditId: {
      type: String,
      default: "",
    },
    addEditStatus: {
      type: String,
      default: "add",
    },
    addEditIsDialog: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      ruleForm: {
        // 表单字段
        id: "",
        companyId: "",
        modelOfflineSettingsId: "",
        deviceGroupId: "",
      },
      rules: {
        // 表单规则
        companyId: [
          { required: true, message: "请选择机构名称", trigger: "change" },
        ],
        modelOfflineSettingsId: [
          { required: true, message: "请选择离线模板", trigger: "change" },
        ],
        deviceGroupId: [
          { required: true, message: "请选择设备分组", trigger: "change" },
        ],
      },
      addEditIsDialog_c: false, // 弹窗是否显示
      seeType: false, // 是否禁用
      companyArray: [], // 机构数组
      modelOfflineSettingsArray: [], // 离线模板数组
      deviceGroupArray: [], // 设备分组数组
      userType: "", // 账号类型（0超级管理员，2组织管理员，3一般管理员）
      companyId: "", // 机构id
    };
  },
  watch: {
    addEditIsDialog(val) {
      this.addEditIsDialog_c = val;
      if (this.addEditIsDialog_c) {
        this.userType = Number(window.localStorage.getItem("userType"));
        this.companyId = Number(window.localStorage.getItem("companyId"));
        if (this.userType == 2) {
          this.ruleForm.companyId = this.companyId;

          if (this.addEditStatus == "add") {
            // 获取离线模板列表
            this.getModelOfflineSettingsList();

            // 获取设备分组列表
            this.getDeviceGroupList();
          }
        }

        // 添加
        if (this.addEditStatus == "add") {
          this.ruleForm.id = "";
          this.seeType = false;
        }

        // 编辑
        if (this.addEditStatus == "edit") {
          this.ruleForm.id = this.addEditId;
          this.seeType = false;
          this.getDetail();
        }

        // 查看
        if (this.addEditStatus == "see") {
          this.ruleForm.id = this.addEditId;
          this.seeType = true;
          this.getDetail();
        }

        // 获取机构列表
        this.getCompanyList();
      }
    },
  },
  methods: {
    // 详情
    getDetail() {
      getModelOfflineGroup({ id: this.addEditId }).then((res) => {
        if (res.code == 200) {
          this.ruleForm = res.data;

          // 获取离线模板列表
          this.getModelOfflineSettingsList();

          // 获取设备分组列表
          this.getDeviceGroupList();
        }
      });
    },
    // 提交
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          saveModelOfflineGroup(this.ruleForm).then((res) => {
            if (res.code == 200) {
              this.$message({
                type: "success",
                message: res.msg,
              });

              // 取消
              this.resetForm(formName);
              // 刷新列表
              this.$parent.onSubmit();
            }
          });
        }
      });
    },
    // 取消
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.modelOfflineSettingsArray = [];
      this.deviceGroupArray = [];
      this.$parent.closeAddEdit();
    },
    // 获取机构列表
    getCompanyList() {
      getCompanyInfoTree().then((res) => {
        if (res.code == 200) {
          this.companyArray = res.data;
        }
      });
    },
    // 机构下拉切换
    companyChange() {
      // 清空
      this.ruleForm.modelOfflineSettingsId = "";
      this.modelOfflineSettingsArray = [];

      this.ruleForm.deviceGroupId = "";
      this.deviceGroupArray = [];

      // 获取离线模板列表
      this.getModelOfflineSettingsList();

      // 获取设备分组列表
      this.getDeviceGroupList();
    },
    // 获取离线模板列表
    getModelOfflineSettingsList() {
      if (!this.ruleForm.companyId || this.ruleForm.companyId == "") {
        return;
      }
      getModelOfflineSettingss({ id: this.ruleForm.companyId }).then((res) => {
        if (res.code == 200) {
          this.modelOfflineSettingsArray = res.data;
        }
      });
    },
    // 获取设备分组列表
    getDeviceGroupList() {
      if (!this.ruleForm.companyId || this.ruleForm.companyId == "") {
        return;
      }
      getDeviceGroups({ id: this.ruleForm.companyId }).then((res) => {
        if (res.code == 200) {
          this.deviceGroupArray = res.data;
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
.demo-ruleForm {
  width: 95%;
  margin: 0 auto;
  padding-top: 10px;
}
</style>
